/*
    This source file is part of GeNa project.
    Copyright © GeNa project 2008
    For the latest info, see http://code.google.com/p/genaproject/ or  http://lumpyproject.forums-rpg.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef IMAGE_H_INCLUDED
#define IMAGE_H_INCLUDED

#include <string>

class Image
{
public:
    GLenum type; //GL_TEXTURE_{1D,2D,3D,CUBE_MAP}

    struct SubImage
    {
        GLenum type;
        GLenum format;
        unsigned int width, height, depth;
        unsigned int mip;
        bool swap; //If true, glPixelStorei( GL_UNPACK_ROW_LENGTH, GL_TRUE ); should be call before loading texture

        unsigned char* buffer;
        unsigned int imageSize; //Used only with compressed image, uncompressed image loader can miss this field

        bool deleteBuffer;

        GLenum face; //For cubemap

        SubImage()
        {
            type = GL_NONE;
            format = GL_NONE;
            width = 1; height = 1; depth = 1;
            swap = false;
            mip = 0;

            imageSize = 0;
            buffer = NULL;
            deleteBuffer = true;
        }

        ~SubImage()
        {
            if( deleteBuffer)
                delete[] buffer;
        }
    };

    SubImage* images; // { f1mip0, f1mip1, …, f2mip0, f2mip1, … }
    unsigned int imgNum;

    bool load( std::string filename);
    bool loadJPEG( std::string filename);
    bool loadPNG( std::string filename);
    bool loadTGA( std::string filename);
    bool loadDDS( std::string filename);

    bool texImage( GLenum target, GLenum internalFormat);
    bool texImage( SubImage* image, GLenum target, int mipmapLevel, GLenum internalFormat);
    bool texSubImage( SubImage* image, GLenum target, int mipmapLevel, unsigned int xOffset, unsigned yOffset, unsigned int zOffset);

    Image()
    {
        type = GL_NONE;

        images = NULL;
        imgNum = 0;
    }

    ~Image()
    {
        delete[] images;
    }
};

#endif // IMAGE_H_INCLUDED
